<?php
App::uses('AppController', 'Controller');
/**
 * Fqas Controller
 *
 * @property Fqa $Fqa
 * @property PaginatorComponent $Paginator
 */
class FqasController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator','Session');
        public $helpers = array('Form', 'Html', 'Js', 'Time','Session');

/**
 * index method
 *
 * @return void
 */
         public function beforeFilter() {

               $this->Auth->allow('display','fqalist','index','add', 'delete', 'edit');
             
                   $this->layout='default';
              
                    
               
                Controller::loadModel('Menu');
                  $this->set('menus', $this->Menu->Find('all', array('conditions'=>array('Menu.position'=>'0'), 'order' => array('Menu.order' => 'asc'))));
                   $this->set('menusbottom', $this->Menu->Find('all', array('conditions'=>array('Menu.position'=>'1'), 'order' => array('Menu.order' => 'asc'))));
                   $this->set('menusleft', $this->Menu->Find('all', array('conditions'=>array('Menu.position'=>'2'), 'order' => array('Menu.order' => 'asc'))));
                    Controller::loadModel('Category');
         Controller::loadModel('Location');
        $nodelist = $this->Category->generateTreeList(null,null,null," - ");

//			$this->set('parents',$this->Category->generatetreelist());
        $this->set('parents',($nodelist));
        $categories = $this->Category->find('list');
        $locations = $this->Location->find('list');
        $this->set(compact('locations'));
        $this->set('params','');
        
        Controller::loadModel('Paid_ads');
        $myDate = date("Y-m-d");
        // Left Ads
        $leftads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 2, 'Paid_ads.expired >=' => $myDate))));
        $this->set('leftbanners', $leftads);

        // Right Ads
        $rightads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 3, 'Paid_ads.expired >=' => $myDate))));
        $this->set('rightbanners', $rightads);

        // Top Header Ads
        $topheaderads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 4, 'Paid_ads.expired >=' => $myDate))));
        $this->set('topheaderads', $topheaderads);

        // Top Header Ads
        $bannerads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 5, 'Paid_ads.expired >=' => $myDate))));
        $this->set('bannerads', $bannerads);

        // Top Content Ads
        $topcontentads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 6, 'Paid_ads.expired >=' => $myDate))));
        $this->set('topcontentads', $topcontentads);

        // Top Content Ads
        $middlecontentads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 7, 'Paid_ads.expired >=' => $myDate))));
        $this->set('middlecontentads', $middlecontentads);

        // Top Content Ads
        $bottomcontentads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 8, 'Paid_ads.expired >=' => $myDate))));
        $this->set('bottomcontentads', $bottomcontentads);
        
        // Left Sidebar Ads
        $leftsidebarads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 9, 'Paid_ads.expired >=' => $myDate))));
        $this->set('leftsidebarads', $leftsidebarads);
        
        // Right Sidebar Ads
        $rightsidebarads = $this->Paid_ads->find('all', array('conditions' => array('AND' => array('Paid_ads.position' => 10, 'Paid_ads.expired >=' => $myDate))));
        $this->set('rightsidebarads', $rightsidebarads);
        }
        public function fqalist() {
            Controller::loadModel('AnsFqa');
            //Controller::loadModel('Fqa');
            $this->layout = 'home';
            $this->AnsFqa->recursive = 0;
            
            $this->set('fqas', $this->Fqa->find('all'));
            $this->set('ans', $this->AnsFqa->find('all'));
          //  $this->set('fqas', $this->Paginator->paginate('AnsFqa'));
//            $this->layout='home';
//		$this->Fqa->recursive = 0;
//		$this->set('fqas', $this->Paginator->paginate());
//        $fqa = $this->AnsFqa->find('all');
//        $this->set('fqas',$fqa);
	}   
	public function index() {
            
		$this->Fqa->recursive = 0;
		$this->set('fqas', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
             
		if (!$this->Fqa->exists($id)) {
			throw new NotFoundException(__('Invalid fqa'));
		}
		$options = array('conditions' => array('Fqa.' . $this->Fqa->primaryKey => $id));
		$this->set('fqa', $this->Fqa->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
             if ($this->Auth->user('role') && $this->Auth->user('role')=='Admin'){
		if ($this->request->is('post')) {
			$this->Fqa->create();
			if ($this->Fqa->save($this->request->data)) {
				$this->Session->setFlash(__('The fqa has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The fqa could not be saved. Please, try again.'));
			}
		}
             }
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
             if ($this->Auth->user('role') && $this->Auth->user('role')=='Admin'){
		if (!$this->Fqa->exists($id)) {
			throw new NotFoundException(__('Invalid fqa'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Fqa->save($this->request->data)) {
				$this->Session->setFlash(__('The fqa has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The fqa could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Fqa.' . $this->Fqa->primaryKey => $id));
			$this->request->data = $this->Fqa->find('first', $options);
		}
             }
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
             if ($this->Auth->user('role') && $this->Auth->user('role')=='Admin'){
		$this->Fqa->id = $id;
		if (!$this->Fqa->exists()) {
			throw new NotFoundException(__('Invalid fqa'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Fqa->delete()) {
			$this->Session->setFlash(__('The fqa has been deleted.'));
		} else {
			$this->Session->setFlash(__('The fqa could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
             }
	}}
